package com.unytech.wisdomcommunity.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.unytech.wisdomcommunity.R;
import com.unytech.wisdomcommunity.mainfragment.BusinessFragment2;
import com.unytech.wisdomcommunity.mainfragment.HomeFragment2;
import com.unytech.wisdomcommunity.mainfragment.MeFragment2;
import com.unytech.wisdomcommunity.mainfragment.PropertyManagementFragment2;

/**
 * TODO 这个是底部有凸起按钮的主界面activity_main2
 */
public class Main2Activity extends Activity implements View.OnClickListener {
    private CharSequence toastStr = "再按一次将退出智慧社区";
    private FrameLayout layout1, layout2,layout3,layout4,layout5;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private ImageView tab_home_iv,tab_pm_iv,tab_shop_iv,frame_mid_iv,tab_me_iv;
    private TextView  tab_home_text, tab_pm_text,tab_shop_text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        InitView();
    }
    @Override
    protected void onStart() {
        super.onStart();
        palyAnmation();
//        Log.d("动画", "动画开始了");
    }
    private void palyAnmation() {
        RotateAnimation rotateAnimation = new RotateAnimation(0,360,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF,0.5f);
        rotateAnimation.setDuration(5000);
        rotateAnimation.setRepeatCount(Animation.INFINITE);
        rotateAnimation.setRepeatMode(Animation.RESTART);
        //匀速转动的动画插值器
        LinearInterpolator linearInterpolator = new LinearInterpolator();
        rotateAnimation.setInterpolator(linearInterpolator);

        frame_mid_iv.startAnimation(rotateAnimation);
    }
    @Override
    protected void onStop() {
        super.onStop();
        frame_mid_iv.clearAnimation();
//        Log.d("动画","动画关闭了");
    }
    private void InitView() {
        layout1=(FrameLayout)findViewById(R.id.frame_home);
        layout2=(FrameLayout)findViewById(R.id.frame_pm);
        layout3=(FrameLayout)findViewById(R.id.frame_shop);
        layout4=(FrameLayout)findViewById(R.id.frame_set);
        layout5=(FrameLayout)findViewById(R.id.frame_home_mid);

        tab_home_iv=(ImageView)findViewById(R.id.tab_home_iv);
        tab_pm_iv=(ImageView)findViewById(R.id.tab_pm_iv);
        tab_shop_iv=(ImageView)findViewById(R.id.tab_shop_iv);
        tab_me_iv=(ImageView)findViewById(R.id.tab_me_iv);
        frame_mid_iv=(ImageView)findViewById(R.id.frame_mid_iv);

        tab_home_text=(TextView)findViewById(R.id.tab_home_text);
        tab_pm_text=(TextView)findViewById(R.id.tab_pm_text);
        tab_shop_text=(TextView)findViewById(R.id.tab_shop_text);

        layout1.setOnClickListener(this);
        layout2.setOnClickListener(this);
        layout3.setOnClickListener(this);
        layout4.setOnClickListener(this);
        layout5.setOnClickListener(this);
        ImageViewClick();
        fragmentManager = getFragmentManager();
        transaction = fragmentManager.beginTransaction();

        tab_home_iv.setImageResource(R.drawable.tab_home_click);
        Fragment fragment = new HomeFragment2();
        //设定fragment的显示位置
        transaction.replace(R.id.main_fragment_container, fragment);
        //提交你的fragment事物
        transaction.commit();
        //为radio设定选中监听器
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.frame_home:
                //setTitle("首页");
                ImageViewClick();
                tab_home_iv.setImageResource(R.drawable.tab_home_click);
                transaction = fragmentManager.beginTransaction();
                Fragment homeFragment = new HomeFragment2();
                transaction.replace(R.id.main_fragment_container, homeFragment);
                transaction.commit();
                break;
            case R.id.frame_pm:
                //setTitle("物业");
                ImageViewClick();
                tab_pm_iv.setImageResource(R.drawable.tab_pm_click);
                transaction = fragmentManager.beginTransaction();
                Fragment pmFragment2 = new PropertyManagementFragment2();
                transaction.replace(R.id.main_fragment_container, pmFragment2);
                transaction.commit();
                break;
            case R.id.frame_shop:
                //setTitle("商业");
                ImageViewClick();
                tab_shop_iv.setImageResource(R.drawable.tab_shop_click);
                transaction = fragmentManager.beginTransaction();
                Fragment businessFragment2 = new BusinessFragment2();
                transaction.replace(R.id.main_fragment_container, businessFragment2);
                transaction.commit();
                break;
            case R.id.frame_set:
                //setTitle("我的");
                ImageViewClick();
                tab_me_iv.setImageResource(R.drawable.tab_me_click);
                transaction = fragmentManager.beginTransaction();
                Fragment meFragment2 = new MeFragment2();
                transaction.replace(R.id.main_fragment_container, meFragment2);
                transaction.commit();
                break;
            case R.id.frame_home_mid:
                Intent intent=new Intent(Main2Activity.this,InteractActivity.class);
                startActivity(intent);
                break;
        }
    }
    private void ImageViewClick() {
//        "#0aca4e"
        tab_home_iv.setImageResource(R.drawable.tab_home);
        tab_pm_iv.setImageResource(R.drawable.tab_pm);
        tab_shop_iv.setImageResource(R.drawable.tab_shop);
        tab_me_iv.setImageResource(R.drawable.tab_me);
        frame_mid_iv.setImageResource(R.drawable.tab_mid);
    }

    /*
    * 连续点击两次物理返回键  退出主程序
    * */
    //上次点击返回键的时间
    private long lastBackPressed;
    //两次点击的间隔时间
    private static final int QUIT_INTERVAL = 2000;
    @Override
    public void onBackPressed() {
        long backPressed = System.currentTimeMillis();
        if (backPressed - lastBackPressed > QUIT_INTERVAL) {
            lastBackPressed = backPressed;
            Toast.makeText(this, toastStr, Toast.LENGTH_SHORT).show();
        } else {
            finish();
            System.exit(0);
        }
    }
}